﻿Public MustInherit Class ExcelCreateBase

    Private _excelApp As Object
    Private _workBook As Object
    Private _filename As String
    Private _currentSheetIndex As Integer = 1

    Public Sub New(ByVal fileName As String)
        _filename = fileName
    End Sub

    Public Function 打开() As Boolean
        _excelApp = CreateObject("Excel.Application")
        _excelApp.UserControl = False

        _workBook = _excelApp.Workbooks.Add()
    End Function

    Public Function 保存() As Boolean

        If _workBook IsNot Nothing Then
            _workBook.SaveAs(_filename)
        End If

    End Function

    Public Function 关闭() As Boolean
        If _workBook IsNot Nothing Then
            _workBook.Close()
        End If
        _workBook = Nothing

        If _excelApp IsNot Nothing Then
            _excelApp.Quit()
        End If

        Try
            Dim generation As Integer = System.GC.GetGeneration(_excelApp)
            _excelApp = Nothing
            System.GC.Collect(generation)
        Catch ex As Exception

        End Try

    End Function

    Public Sub SetCurrentSheetTitle(ByVal caption As String)
        'GetCurrentWorkSheet.
    End Sub

    Public Function GetCurrentWorkSheet() As Object
        If _workBook.Sheets.Count < _currentSheetIndex Then
            _workBook.Sheets.Add(, _workBook.Sheets.Item(_workBook.Sheets.Count))
        End If

        Dim ws As Object = _workBook.Sheets.Item(_currentSheetIndex)
        If Not _excelApp.ActiveSheet.Equals(ws) Then
            ws.Select()
        End If

        Return _workBook.Sheets.Item(_currentSheetIndex)

    End Function

    Public Function GetCurrentWorkBook() As Object
        Return _workBook
    End Function

    Public Function CentimetersToPoints(ByVal c As Decimal) As Single

        Return CType(c * 28.35, Single)

    End Function

    Public Property CurrentSheetIndex() As Integer
        Get
            Return _currentSheetIndex
        End Get
        Set(ByVal value As Integer)
            _currentSheetIndex = value
        End Set
    End Property

    MustOverride Property 报表字体() As String
    MustOverride Property 报表记录条数字() As Integer
End Class
